package com.xiongwei.collection;

import com.xiongwei.pojo.People;
import com.xiongwei.pojo.Student;

import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Queue;

/**
 * PriorityQueue实现了一个优先队列:从队首获取元素时,总是获取优先级最高的元素
 * PriorityQueue默认按照元素比较的顺序排序(必须实现Comparable接口)
 *  ,也可通过Comparator自定义排序算法(元素可不实现Comparable接口)
 *
 * @author XiongWei
 * @date 2021/1/7
 */
public class PriorityQueueUse {

    /**
     * 银行排队业务为例
     * @param args
     */
    public static void main(String[] args) {
        Queue<People> q = new PriorityQueue<>(new PersonComparator());
        // 添加3个元素到队列:
        q.offer(new People("Bob", "A1"));
        q.offer(new People("Alice", "A2"));
        q.offer(new People("Boss", "V1"));
        System.out.println(q.poll()); // Boss/V1
        System.out.println(q.poll()); // Bob/A1
        System.out.println(q.poll()); // Alice/A2
        System.out.println(q.poll()); // null,因为队列为空
    }
}

//如果两人的号都是A开头或者都是V开头,比较号的大小
//V的优先级高
class PersonComparator implements Comparator<People> {
    @Override
    public int compare(People o1, People o2) {
        if (o1.number.charAt(0) == o2.number.charAt(0)) {
            return o1.number.compareTo(o2.number);
        }
        if (o1.number.charAt(0) == 'V') {
            return -1;
        } else {
            return 1;
        }
    }
}
